package com.example.academicsystem.server.service.impl;

import com.example.academicsystem.constant.MessageConstant;
import com.example.academicsystem.exception.DeleteException;
import com.example.academicsystem.exception.InsertException;
import com.example.academicsystem.exception.QueryFailedException;
import com.example.academicsystem.pojo.dto.*;
import com.example.academicsystem.pojo.vo.CourseTableVo;
import com.example.academicsystem.pojo.vo.CourseVo;
import com.example.academicsystem.pojo.vo.StudentGradeVo;
import com.example.academicsystem.result.PageResult;
import com.example.academicsystem.server.dao.StudentMapper;
import com.example.academicsystem.server.service.StudentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 查询所有成绩
     * @param studentId
     * @return
    * */
    public List<StudentGradeVo> gradeSelectAll(String studentId) {
        if(studentId==null)
            throw new QueryFailedException(MessageConstant.PARAM_NULL);
        List<StudentGradeVo> studentGradeVoList=studentMapper.gradeSelectAll(studentId);
        if (studentGradeVoList==null)
            throw new QueryFailedException(MessageConstant.QUERY_FAIL);
        return studentGradeVoList;

    }

    /**
     * 成绩分页查询
     * @param studentGradePageQueryDto
     * @return
     * */
    public PageResult gradePageQuery(StudentGradePageQueryDto studentGradePageQueryDto) {
        if(studentGradePageQueryDto.getStudentId()==null||studentGradePageQueryDto.getStudentId()=="")
            throw new QueryFailedException(MessageConstant.PARAM_NULL);
        PageHelper.startPage(studentGradePageQueryDto.getPage(),studentGradePageQueryDto.getPageSize());
        Page<StudentGradeVo> page = studentMapper.gradePageQuery(studentGradePageQueryDto);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据课程名称查询成绩
    * */
    public PageResult studentGradeByCourseName(StudentGradePageQueryDto studentGradePageQueryDto) {
        PageHelper.startPage(studentGradePageQueryDto.getPage(),studentGradePageQueryDto.getPageSize());
        Page<StudentGradeVo> page=studentMapper.studentGradeByCourseName(studentGradePageQueryDto);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 分页查询所有课程
     * */
    public PageResult studentCoursePageQuery(CourseDto courseDto) {
        if(courseDto.getId()==null || courseDto.getId()=="")
            throw new QueryFailedException(MessageConstant.PARAM_NULL);
        PageHelper.startPage(courseDto.getPage(),courseDto.getPageSize());
        List<String> courseIdList = studentMapper.studentCourseSelectAll(courseDto.getId());
        if (courseIdList.size()==0) return null;
        Page<CourseVo> page =studentMapper.coursePageQuery(courseIdList,courseDto.getCourseName(),courseDto.getCourseId());
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据学期查询课表
     * */
    public List<CourseTableVo> CourseTableQuery(CourseTableDto courseTableDto) {
        if (courseTableDto==null||courseTableDto.getId()==null){
            throw new QueryFailedException(MessageConstant.QUERY_FAIL);
        }
        List<String> courseIdList=studentMapper.studentCourseSelectAll(courseTableDto.getId());
        if (courseIdList.size()==0) return null;
        List<CourseTableVo> courseTableVoList =studentMapper.courseTableQuery(courseIdList,courseTableDto.getTerm());
        return courseTableVoList;
    }

    /**
     * student-course 新增
     * */
    public void studentCourseInsert(StudentCourseInsertDto studentCourseInsertDto) {
        if (studentCourseInsertDto.getStudentId()==null||studentCourseInsertDto.getCourseId()==null||studentCourseInsertDto.getStudentId()=="")
            throw new InsertException(MessageConstant.PARAM_NULL);
        studentMapper.studentCourseInsert(studentCourseInsertDto);
    }

    /**
     *student-course 删除
     **/
    public void courseDelete(StudentCourseDeleteDto studentCourseDeleteDto) {
        if (studentCourseDeleteDto.getCourseId()==null || studentCourseDeleteDto.getStudentId()==null){
            throw new DeleteException(MessageConstant.DELETE_FAILED);
        }
        studentMapper.courseDelete(studentCourseDeleteDto);
    }
}
